//
//  WKContenCellPhotoView.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/5.
//  Copyright © 2018年 何文虎. All rights reserved.
//

#import "WKContenCellPhotoView.h"

@interface WKContenCellPhotoView () <SDPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *imageViewsArray;
@end

@implementation WKContenCellPhotoView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)init{
    if (self == [super init]) {
        [self setup];
    }
    return self;
}



- (void)setup
{
//    NSMutableArray *temp = [NSMutableArray array];
//    self.userInteractionEnabled = YES;
//    for (int i = 0; i < 9; i++) {
//        UIImageView *imageView = [UIImageView new];
//        [self addSubview:imageView];
//        imageView.userInteractionEnabled = YES;
//        imageView.tag = i;
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
//        [imageView addGestureRecognizer:tap];
//        [temp addObject:imageView];
//    }
//
//    self.imageViewsArray = [temp copy];
}

- (void)setPicPathStringsArray:(NSArray *)picPathStringsArray{
    
    _picPathStringsArray = picPathStringsArray;
    CGFloat itemW = (ScreenWidth-24-10)/3;
    for (int i = 0; i<picPathStringsArray.count; i++) {
        NSInteger index = i % 3;
        NSInteger page = i /3;
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.backgroundColor = [UIColor blackColor];
        imageV.frame = CGRectMake(index*(itemW+5), page*(5+itemW), itemW, itemW);
        [imageV sd_setImageWithURL:[NSURL URLWithString:picPathStringsArray[i]]];
     
        [imageV setContentScaleFactor:[[UIScreen mainScreen] scale]];
        imageV.contentMode =  UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds  = YES;
        imageV.layer.masksToBounds = YES;
        imageV.layer.cornerRadius = 5;
        
        [self addSubview:imageV];
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];
        [imageV addGestureRecognizer:singleTap];
        
        imageV.tag = i;
        imageV.userInteractionEnabled = YES;
    }
}


- (void)wh_setPicPathStringsArray:(NSArray *)picPathStringsArray
{
    _picPathStringsArray = picPathStringsArray;
    
    for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
        UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
        imageView.hidden = YES;
    }
    
    if (_picPathStringsArray.count == 0) {
        self.height = 0;
        return;
    }
    
//    CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
    CGFloat itemW = (ScreenWidth-24-10)/3;
    CGFloat itemH = 0;
    if (_picPathStringsArray.count == 1) {
        UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
        if (image.size.width) {
            itemH = image.size.height / image.size.width * itemW;
        }
    } else {
        itemH = itemW;
    }
    long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
    CGFloat margin = 5;
    
    [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex = idx / perRowItemCount;
        UIImageView *imageView = [self->_imageViewsArray objectAtIndex:idx];
        imageView.hidden = NO;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 5;
        [imageView sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil];
        imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
        imageView.userInteractionEnabled = YES;
        imageView.tag = idx;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [imageView addGestureRecognizer:tap];
        
        self.backgroundColor = [UIColor greenColor];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = idx;
        btn.frame = imageView.bounds;
        [imageView addSubview:btn];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
//    CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
    int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
//    self.width = w;
    self.height = h;
    
//    self.fixedHeight = @(h);
//    self.fixedWidth = @(w);
}


#pragma mark - 宽高设置
- (CGFloat)itemWidthForPicPathArray:(NSArray *)array
{
    if (array.count==1) {
        return 200;
    }else if (array.count ==2 || array.count ==4){
        return 170;
    }else{
        return (ScreenWidth-24-10)/3;
    }
}

- (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
{
    if (array.count <= 3) {
        return array.count;
    } else if (array.count <= 4) {
        return 2;
    } else {
        return 3;
    }
}




#pragma mark - private actions

- (void)btnAction:(UIButton*)sender{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = sender.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.picPathStringsArray.count;
    browser.delegate = self;
    [browser show];
}

- (void)tapImageView:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.picPathStringsArray.count;
    browser.delegate = self;
    [browser show];
}


#pragma mark - SDPhotoBrowserDelegate

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *imageName = self.picPathStringsArray[index];
    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}


#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    if ([touch.view isKindOfClass:[UIImageView class]]) {
        return YES;
    }else{
        return NO;
    }
    
}



@end
